<style>
  .label {
    display: inline-block;
    margin: 10px 10px 0 0;

  }
</style>
<template>
  <div class="mod-log">
    <el-card class="box-card">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <span class="label">筛选时间</span>
          <el-date-picker
              v-model="time"
              type="month"
              :clearable="false"
              :editable="false"
              placeholder="选择月">
            </el-date-picker>
            <br>
          <br/>
          <el-form-item label="客户发布">
               <el-select v-model="nickname" placeholder="请选择" clearable>
                <el-option
                  v-for="item in buyersList"
                  :key="item.uid"
                  :value="item.nickname">
                </el-option>
              </el-select>
            </el-form-item>
          <!-- <el-input v-model="dataForm.key" placeholder="用户名／用户操作" clearable></el-input> -->
        </el-form-item>
        <br/>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="exportExecl()">导出execl</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%">
      <!-- <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column> -->
      <el-table-column
        prop="nickname"
        header-align="center"
        align="center"
        label="用户名称">
      </el-table-column>
      <el-table-column
        prop="mobileNo"
        header-align="center"
        align="center"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="productCount"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="商品当月数量 ">
      </el-table-column>
      <el-table-column
        prop="totalPrice"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="订单总价格">
      </el-table-column>
      <el-table-column
        prop="orderCount"
        header-align="center"
        align="center"
        label="订单总数量">
      </el-table-column>
      <el-table-column
        prop="bonus"
        header-align="center"
        align="center"
        width="150"
        label="佣金">
      </el-table-column>
      <el-table-column
        prop="productTotalCount"
        header-align="center"
        align="center"
        width="180"
        label="商品总数量">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  import moment  from "moment";
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        selectionDataList: [],

        time:new Date(),
        nickname:"",
        buyersList:[],
      }
    },
    created () {
      this.getDataList()
      this.$http({
          url: this.$http.adornUrl(`/biz/common/getBuyers`),
          method: 'get',
          params: this.$http.adornParams()
      }).then(({data}) => {
          console.log(data);
          this.buyersList = data.data;
      })
    },
    methods: {
      //导出execl
      exportExecl(){
        var uid = "";
        if(this.nickname){
            this.buyersList.map(con => {
              if(this.nickname == con.nickname){
                uid = con.uid;
                return;
              } 
            });
        }
        this.$http({
          url: this.$http.adornUrl('/biz/buyerStatistics/export'),
          method: 'get',
          params: this.$http.adornParams({
            // 'page': this.pageIndex,
            // 'limit': this.pageSize,
            'dt': moment(this.time).format('YYYY-MM'),
            'uid': uid,
          }),
          responseType: 'arraybuffer'
        }).then(({data}) => {
          var blob = new Blob([data], {type: 'application/vnd.ms-excel;charset=utf-8'})
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.href = href;
          downloadElement.download = `买手统计${moment(this.time).format('YYYY-MM')}月.xlsx`; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
          // 
        })
      },
      // 获取数据列表
      getDataList () {
        var uid = "";
        if(this.nickname){
            this.buyersList.map(con => {
              if(this.nickname == con.nickname){
                uid = con.uid;
                return;
              } 
            });
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/biz/buyerStatistics/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'dt': moment(this.time).format('YYYY-MM'),
            'uid': uid,
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      }
    }
  }
</script>
